package com.scvtc.controller.system;

import com.scvtc.domain.ResResult;
import com.scvtc.domain.dto.system.role.RoleMenuIdsDto;
import com.scvtc.domain.dto.system.role.SystemRoleDto;
import com.scvtc.domain.vo.PageVo;
import com.scvtc.domain.vo.system.user.UserRoleVo;
import com.scvtc.service.system.SystemRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Digits;
import java.util.List;

import static com.scvtc.constants.SystemCheckedConstants.PAGE_CURRENT_ERROR;
import static com.scvtc.constants.SystemCheckedConstants.PAGE_SIZE_ERROR;
import static com.scvtc.constants.SystemConstants.PAGE_CURRENT;
import static com.scvtc.constants.SystemConstants.PAGE_SIZE;


/**
 * 角色管理(SystemRole)表控制层
 *
 * @author yiFei
 * @since 2022-04-13 17:38:28
 */
@Api(tags = "角色管理(SystemRole)")
@RestController
@RequestMapping("systemRole")
public class SystemRoleController {
    /**
     * 角色管理(SystemRole)对象
     */
    @Autowired
    private SystemRoleService systemRoleService;

    @GetMapping("/list")
    @ApiOperation("查询所有角色")
    @PreAuthorize("hasAuthority('system:role:list')")
    public ResResult<PageVo<List<SystemRoleDto>>> listRole(
            @Digits(integer = 5, fraction = 1, message = PAGE_CURRENT_ERROR) @RequestParam(defaultValue = PAGE_CURRENT) Long current,
            @Digits(integer = 3, fraction = 0, message = PAGE_SIZE_ERROR) @RequestParam(defaultValue = PAGE_SIZE) Long size,
            @RequestParam(required = false) String name
    ) {
        return systemRoleService.listRole(current, size , name);
    }

    /**
     * 主要给用户分配角色使用
     * @return
     */
    @GetMapping("/all")
    @ApiOperation("获取全部角色")
    public ResResult<List<UserRoleVo>> getAllRole(){
        return systemRoleService.getAllRole();
    }

    @PutMapping("/")
    @ApiOperation("修改角色信息")
    @PreAuthorize("hasAuthority('system:role:update')")
    public ResResult<?> updateRole(@RequestBody SystemRoleDto systemRoleDto) {
        return systemRoleService.updateRole(systemRoleDto);
    }

    @Transactional
    @PutMapping("/update/menu")
    @ApiOperation("根据菜单 ids 修改角色信息")
    @PreAuthorize("hasAuthority('system:role:update')")
    public ResResult<?> updateRoleByMenuIds(@RequestBody RoleMenuIdsDto roleMenuIdsDto) {
        return systemRoleService.updateRoleByMenuIds(roleMenuIdsDto.getRoleId(), roleMenuIdsDto.getMenuIds());
    }

    @PostMapping("/")
    @ApiOperation("添加角色")
    @PreAuthorize("hasAuthority('system:role:add')")
    public ResResult<?> addRole(@RequestBody SystemRoleDto systemRoleDto) {
        return systemRoleService.addRole(systemRoleDto);
    }

    @GetMapping("/menu")
    @ApiOperation("用角色 id 获取菜单")
    @PreAuthorize("hasAuthority('system:role:list')")
    public ResResult<List<Long>> getMenuByRoleId(Long roleId) {
        return systemRoleService.getMenuByRoleId(roleId);
    }



    @DeleteMapping("/")
    @ApiOperation("删除角色")
    @PreAuthorize("hasAuthority('system:role:delete')")
    public ResResult<?> deleteRoleById(Long id) {
        return systemRoleService.deleteRoleById(id);
    }
}

